package com.achun.getcouponleague.ui.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.achun.getcouponleague.Bean.Category;
import com.achun.getcouponleague.R;
import com.achun.getcouponleague.base.BaseFragment;
import com.achun.getcouponleague.base.ISwitchfragment;
import com.achun.getcouponleague.presenter.impl.HomePresenter;
import com.achun.getcouponleague.ui.adapter.ViewPagerAdapter;
import com.achun.getcouponleague.view.IHomeCallback;
import com.google.android.material.tabs.TabLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Home_Fragment extends BaseFragment implements IHomeCallback {

    private HomePresenter homePresenter;

    public TabLayout tabLayout;

    public ViewPager homePager;
    private ViewPagerAdapter viewPagerAdapter;
    @BindView(R.id.search_edid)
    public EditText SearchEdit;

    @Override
    public View loadSuccessView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home_,container,false);
    }

    @Override
    protected View createRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.home_null_fragment,container,false);
    }

    @Override
    protected void onReTry() {
        homePresenter.getCategories();
    }

    @Override
    protected void initView(View rootview) {
//布局都被addview了
//        初始化布局事件
        tabLayout=rootview.findViewById(R.id.sort_view);
        homePager=rootview.findViewById(R.id.main_content_viewpager);
        tabLayout.setupWithViewPager(homePager);
        //创建viewpagerAdapter
        viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
        homePager.setAdapter(viewPagerAdapter);



    }

    @Override
    protected void initListener() {
        //切换页面
        SearchEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentActivity activity = getActivity();
                if (activity instanceof ISwitchfragment){
                    ISwitchfragment iSwitchfragment= (ISwitchfragment) activity;
                    iSwitchfragment.switchFragment();
                }
            }
        });
    }

    @Override
    protected void loadData() {
        //加载数据
        homePresenter.getCategories();

    }

    @Override
    protected void initPresenter() {
        //创建Presenter
        homePresenter = new HomePresenter();
        homePresenter.registerCallback(this);

    }

    @Override
    public void onCategoriesLoaded(Category category) {
        //加载数据回调
        viewPagerAdapter.setDatas(category);
        setViewStatus(Status.SUCCESS);
    }

    @Override
    public void onLoading() {
        setViewStatus(Status.LOADING);
    }

    @Override
    public void onError() {
        setViewStatus(Status.ERROR);

    }

    @Override
    public void onEmpty() {
        setViewStatus(Status.EMPTY);

    }

    @Override
    protected void release() {
        if (homePresenter != null) {
            //取消注册
            homePresenter.unRegisterCallback(this);
        }
    }
}